package com.tuling.controller;

import com.alibaba.excel.EasyExcel;
import com.tuling.common.Result;
import com.tuling.pojo.QuestionAnswer;
import com.tuling.pojo.excel.QuestionAnswerExcel;
import com.tuling.pojo.vo.PersonQuestionAnswerVO;
import com.tuling.pojo.vo.QuestionAnswerVO;
import com.tuling.service.QuestionAnswerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Api(tags = "组员答案维护表")
@RestController
public class QuestionAnswerController {

    @Resource
    private QuestionAnswerService questionAnswerService;

    @ApiOperation(value = "问题回答", notes = "问题回答")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name",
                    value = "用户名",
                    required = true,
                    paramType = "query"
            ), @ApiImplicitParam(name = "password",
            value = "密码",
            required = true,
            paramType = "query"
    ), @ApiImplicitParam(name = "answer",
            value = "问题答案",
            required = true,
            paramType = "query"
    )
    })
    @GetMapping("/upload_answer")
    public Result<Map<String, QuestionAnswerVO>> uploadAnswer(String name, String password, String answer) {
        return questionAnswerService.uploadAnswer(name, password, answer);
    }

    @ApiOperation(value = "报表导出", notes = "报表导出")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name",
                    value = "用户名",
                    required = true,
                    paramType = "query"
            ), @ApiImplicitParam(name = "password",
            value = "密码",
            required = true,
            paramType = "query"
    ), @ApiImplicitParam(name = "type",
            value = "不填写默认统计全部类型情况，类型（JVM,KafKa,MQ,MySQL,Redis,Spring,ZK,分库分表,并发专题,微服务,计算机基础）",
            required = false,
            paramType = "query"
    )
    })
    @GetMapping("export")
    public void export(HttpServletResponse response, String name, String password, String type) throws IOException {
        Result<List<QuestionAnswerExcel>> export = questionAnswerService.export(name, password, type);
        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode(name + "情况统计", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");

        EasyExcel.write(response.getOutputStream(), QuestionAnswerExcel.class).sheet(name + "复习情况汇总").doWrite(export.getData());
    }

    @ApiOperation(value = "评分", notes = "评分")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",
                    value = "questionId",
                    required = false,
                    paramType = "query"
            ),@ApiImplicitParam(name = "scoreNum",
                    value = "评分",
                    required = false,
                    paramType = "query"
            ),@ApiImplicitParam(name = "remark",
                    value = "备注",
                    required = false,
                    paramType = "query"
            ),@ApiImplicitParam(name = "useName",
                    value = "用户名",
                    required = true,
                    paramType = "query"
            ), @ApiImplicitParam(name = "password",
            value = "密码",
            required = true,
            paramType = "query"
    )})
    @GetMapping("score")
    public Result<PersonQuestionAnswerVO> score(Long id, Integer scoreNum, String remark, String useName, String password) {
        return questionAnswerService.score(id, scoreNum, remark, useName, password);
    }

    @ApiOperation(value = "每日答案导出", notes = "每日答案导出")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "data",
                    value = "导出时间格式（yyyy-MM-dd），为空则导出当天数据",
                    required = false,
                    paramType = "query"
            )
    })
    @GetMapping("daily_person_export")
    public Result<List<QuestionAnswerExcel>> dailyPersonExport(HttpServletResponse response, String data) throws IOException, ParseException {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        // 当天字符串时间
        String daily = simpleDateFormat.format(calendar.getTime());
        data = StringUtils.isBlank(data) ? daily : data;
        calendar.setTime(date);
        Result<List<QuestionAnswerExcel>> export = questionAnswerService.dailyPersonExport(data);
        if (export.isSuccess()) {
            // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("发际线保护协会" + data, "UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(), QuestionAnswerExcel.class).sheet(data).doWrite(export.getData());
        } else {
            return export;
        }
        return Result.ok(null);
    }
}
